package com.rzj.book.dao;

import com.rzj.book.en.Menu;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @ClassName IMenuDao
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/17 17:05
 * @Version 1.0
 */
@Mapper
public interface IMenuDao {

    @Select("select * from menu where menuId = #{menuId}")
    @Results({
            @Result(id=true,column="menuid",property="menuid"),
            @Result(column="menuName",property="menuName"),
            @Result(column="icon",property="icon"),
            @Result(column="url",property="url"),
            @Result(column="pid",property="menu",one = @One(select = "com.rzj.book.dao.IMenuDao.getMenuById",fetchType = FetchType.LAZY)),
            @Result(column="menuId",property="menus",many=@Many(select="com.rzj.book.dao.IMenuDao.getMenuChildrenListById",fetchType = FetchType.LAZY))
    })
    Menu getALLMenu(Integer menuId);

    @Select("select * from menu where menuId = #{menuId}")
    Menu getMenuById(Integer menuId);

    @Select("select * from menu where pid = #{menuId}")
    @Results({
            @Result(id=true,column="menuid",property="menuid"),
            @Result(column="menuName",property="menuName"),
            @Result(column="icon",property="icon"),
            @Result(column="url",property="url"),
            @Result(column="pid",property="menu",one = @One(select = "com.rzj.book.dao.IMenuDao.getMenuById",fetchType = FetchType.LAZY)),
            @Result(column="menuId",property="menus",many=@Many(select="com.rzj.book.dao.IMenuDao.getMenuChildrenListById",fetchType = FetchType.LAZY))
    })
    List<Menu> getMenuChildrenListById(Integer menuId);

    //根据用户id查询角色
    @Select(value=" SELECT m.* FROM menu m " +
            " LEFT JOIN role_menu rm ON rm.menuId = m.menuId " +
            " LEFT JOIN role r on r.roleId = rm.roleId " +
            " LEFT JOIN emp_role er ON er.roleId = r.roleId " +
            " LEFT JOIN emp e ON e.empId = er.empId  " +
            " where e.empId = #{empId} ")
    @Results({
            @Result(id=true,column="menuid",property="menuid"),
            @Result(column="menuName",property="menuName"),
            @Result(column="icon",property="icon"),
            @Result(column="url",property="url"),
            @Result(column="pid",property="menu",one = @One(select = "com.rzj.book.dao.IMenuDao.getMenuById",fetchType = FetchType.LAZY)),
            @Result(column="menuId",property="menus",many=@Many(select="com.rzj.book.dao.IMenuDao.getMenuChildrenListById",fetchType = FetchType.LAZY))
    })
    List<Menu> findMenuListByEmpId(Integer empId);

    //根据角色id查询菜单集合
    @Select(value=" SELECT m.* from Menu m LEFT JOIN role_menu rm on rm.menuId = m.menuId " +
            " LEFT JOIN role r on r.roleId = rm.roleId " +
            " where r.roleId = #{roleId} ")
    List<Menu> findMenuListByRoleId(Integer roleId);
}
